﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.CodeDom;
using NRemedy.CodeGenerate;

namespace NRemedy.CodeGenerator.WinForm
{
    public class CodeGenerateThread : ARThread
    {
        private string input = string.Empty;

        public PushCodeDelegate GenerateCompeleted;

        public CodeGenerateThread()
        {
            GenerateCompeleted += new PushCodeDelegate(CodeGenereteThread_GenerateCompeleted);
        }

        protected virtual void CodeGenereteThread_GenerateCompeleted(string code)
        {
 
        }

        public void Start(params object[] param)
        {
            _thread = new Thread(GenerateCode);
            input = param[0].ToString();
            _thread.Start();
        }


        private void GenerateCode()
        {

            CSharpModelGenerator generator = new CSharpModelGenerator(_context);
            CSharpModelGeneratorDefaultFactory csmgdf = new CSharpModelGeneratorDefaultFactory();
            CodeCompileUnit ccu = generator.GenerateClassByFormName(input,csmgdf);
            //string code = generator.GenerateClassByFormName(input);
            string code = generator.GenerateCodeByCodeCompileUnit(ccu);
            if (GenerateCompeleted != null)
            {
                Control control = GenerateCompeleted.Target as Control;
                if (control != null)
                    control.Invoke(GenerateCompeleted, code);
                else
                    GenerateCompeleted(code);
            }
        }

        
    }
}
